package com.ttmall.ware.service.impl;

import com.ttmall.common.utils.R;
import com.ttmall.ware.feign.ProductFeignService;
import com.ttmall.ware.vo.SkuHasStockVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttmall.common.utils.PageUtils;
import com.ttmall.common.utils.Query;

import com.ttmall.ware.dao.WareSkuDao;
import com.ttmall.ware.entity.WareSkuEntity;
import com.ttmall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();

        String skuId = (String) params.get("skuId");
        String wareId = (String) params.get("wareId");

        if (!StringUtils.isEmpty(skuId)){
            queryWrapper.eq("sku_id" , skuId);
        }

        if (!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id" , wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {

        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("ware_id", wareId).eq("sku_id", skuId));

        if (wareSkuEntities== null ||  wareSkuEntities.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId( skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock( skuNum);
            wareSkuEntity.setStockLocked(0);

            try {
                R info = productFeignService.info(skuId);
                if (info.getCode() == 0){
                    Map<String , Object> map = (Map<String, Object>) info.get("skuInfo");

                    wareSkuEntity.setSkuName((String) map.get("skuName"));
                }
            }
            catch (Exception ex){

            }


            wareSkuDao.insert(wareSkuEntity);
        }
        else {
            wareSkuDao.addStock(skuId,wareId , skuNum);
        }


    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {

        List<SkuHasStockVo> collect = skuIds.stream().map(sku -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            Long count = baseMapper.getSkuStock(sku);
            vo.setSkuId(sku);
            vo.setHasStock(count == null ? false: count > 0);
            return vo;
        }).collect(Collectors.toList());
        return  collect;
    }

}